package com.nova.android.tvlauncher.ui.fragment.atv

import android.os.Bundle
import androidx.core.view.isVisible
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.bean.atv_bean.CusChanelDto
import com.nova.android.tvlauncher.databinding.FragmentCustomChannelAtvBinding
import com.nova.android.tvlauncher.databinding.ItemChannelParentBinding
import com.nova.android.tvlauncher.ext.navigateTo
import com.nova.android.tvlauncher.constants.ConstantsAtv
import com.nova.android.tvlauncher.ui.adapter.atv.HomeListAtvAdapter
import com.shudong.lib_base.ext.appContext
import com_github_lib_base.base.BaseVMFragment
import com_github_lib_base.base.BaseViewModel
import com_github_lib_base.ext.clickNoRepeat
import com_github_lib_base.ext.getAppIconByPackage
import com_github_lib_base.ext.getAppNameWithCache
import com_github_lib_base.ext.rv.setup
import com_github_lib_base.ext.stringValueWithFormat
import sendLiveEventData

class CustomChannelATVFragment : BaseVMFragment<FragmentCustomChannelAtvBinding, BaseViewModel>() {

    override fun initView() {

        val channels = AppCache.channelList

       /* val channelData = ChanelData("推荐视频",true)
        val channelData1 = ChanelData("时下流行",false)
        val channelData2 = ChanelData("Action",true)
        val channelDataList = mutableListOf(channelData,channelData1)
        val channelDataList1 = mutableListOf(channelData2)
        val cusChannelDto = CusChanelDto("Youtube","com.nova.android.tvlauncher","${channelDataList.size}个频道",channelDataList)
        val cusChannelDto1 = CusChanelDto("ChannelSample","com.nova.android.tvlauncher",channelData2.channelName,channelDataList1)
        val cusChannelDtoList = mutableListOf(cusChannelDto,cusChannelDto1)*/
        mBind.rvChannel.setup(channels) {

                addType(CusChanelDto::class, R.layout.item_channel_parent)
                onBind {
                    val dto = data as CusChanelDto
                    val binding = getBinding<ItemChannelParentBinding>()

                    if(dto.packageName=="com.google.android.tvrecommendations"){
                        val name = "com.android.vending"
                        binding.ivBannerIcon.setImageDrawable(name.getAppIconByPackage(
                            appContext
                        ))
                        binding.tvChannelName.text = name.getAppNameWithCache()
                    }else{
                        binding.tvChannelName.text = dto.appName
                        binding.ivBannerIcon.setImageDrawable(dto.packageName.getAppIconByPackage(
                            appContext
                        ))
                    }

                    if(dto.channelDataList.size>1){
                        binding.tvChannelCount.text = com.nova.lib_lang.R.string.cuschannel_txt_4.stringValueWithFormat(dto.channelDataList.size.toString())
                    }else{
                        binding.tvChannelCount.text = "${dto.channelDataList[0].channelName}"
                    }

                    binding.switchWidget.isVisible = dto.channelDataList.size==1
                    binding.ivRightArrow.isVisible = dto.channelDataList.size>1

                    if(binding.switchWidget.isVisible)binding.switchWidget.isChecked =
                        dto.channelDataList[0].isShowChannelData

                    itemView.clickNoRepeat {
                        if(dto.channelDataList.size>1){
                            requireActivity().supportFragmentManager.navigateTo(R.id.fl_container,
                                ChannelChildATVFragment.newInstance(dto.packageName))
                        }else{
                            dto.channelDataList[0].isShowChannelData = !dto.channelDataList[0].isShowChannelData
                            binding.switchWidget.isChecked = !binding.switchWidget.isChecked
                            HomeListAtvAdapter.updateWithCopy(
                                dto.packageName ?: "",
                                dto.channelDataList[0].channelId ?: 0,
                                dto.channelDataList[0].isShowChannelData
                            )
                            sendLiveEventData(
                                ConstantsAtv.UPDATE_HOME_ITEM,
                                Pair(dto.channelDataList[0].isShowChannelData, dto.channelDataList[0].channelId)
                            )
                        }
                    }
                }

        }
    }



    companion object {
        @JvmStatic
        fun newInstance() =
            CustomChannelATVFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }

}